library(vars)
library(astsa)
library(forecast)
library(tseries)
library(dplyr)
library(ggplot2)
library(lubridate)
library(data.table)
library(ggpubr)
# read data
df = read.table(file = '2004-2019.tsv', sep = '\t', header = TRUE)
df['X']=NULL
colnames(df) = c("start_date", "end_date","region", "state", "fuel", "num_gas_stations",
                   "unit", "avg_price","sd_price", "min_price","max_price","avg_price_margin",
               "coef_dist","dist_avg_price","dist_sd_price","dist_min_price", "dist_max_price",
                "coef_price", "year",  "month")
# rename the data

df = df %>% mutate(region = recode(region, "SUL"="South", "SUDESTE"="Southeast", "CENTRO OESTE"="Midwest", 
            "NORTE"="North", "NORDESTE"="Northeast"))
table(df$region)
## 
##   Midwest Northeast     North Southeast     South 
##     15429     36869     24702     17047     12776
df = df %>% mutate(fuel = recode(fuel, "ÓLEO DIESEL"="Diesel", "GASOLINA COMUM"="Regular Gasoline", "GLP"="LPG", "ETANOL HIDRATADO"="Hydrous Ethanol", "GNV"="Natural Gas", "ÓLEO DIESEL S10"="Diesel S10"))
#table(df$fuel)

#df[df$fuel=='LPG',]
colnames(df)
##  [1] "start_date"       "end_date"         "region"          
##  [4] "state"            "fuel"             "num_gas_stations"
##  [7] "unit"             "avg_price"        "sd_price"        
## [10] "min_price"        "max_price"        "avg_price_margin"
## [13] "coef_dist"        "dist_avg_price"   "dist_sd_price"   
## [16] "dist_min_price"   "dist_max_price"   "coef_price"      
## [19] "year"             "month"
# compute the average gas price 

str(df)
## 'data.frame':    106823 obs. of  20 variables:
##  $ start_date      : Factor w/ 785 levels "2004-05-09","2004-05-16",..: 1 1 1 1 1 1 1 1 1 1 ...
##  $ end_date        : Factor w/ 785 levels "2004-05-15","2004-05-22",..: 1 1 1 1 1 1 1 1 1 1 ...
##  $ region          : Factor w/ 5 levels "Midwest","Northeast",..: 1 1 1 1 2 2 2 2 2 2 ...
##  $ state           : Factor w/ 27 levels "ACRE","ALAGOAS",..: 7 9 11 12 2 5 6 10 15 17 ...
##  $ fuel            : Factor w/ 6 levels "Hydrous Ethanol",..: 1 1 1 1 1 1 1 1 1 1 ...
##  $ num_gas_stations: int  127 387 192 162 103 408 278 105 125 423 ...
##  $ unit            : Factor w/ 3 levels "R$/13Kg","R$/l",..: 2 2 2 2 2 2 2 2 2 2 ...
##  $ avg_price       : num  1.29 1.16 1.39 1.26 1.18 ...
##  $ sd_price        : num  0.016 0.114 0.097 0.07 0.078 0.132 0.218 0.158 0.13 0.141 ...
##  $ min_price       : num  1.19 0.89 1.18 1.09 1.05 0.999 1.03 1.35 1.1 0.989 ...
##  $ max_price       : num  1.35 1.45 1.76 1.51 1.4 ...
##  $ avg_price_margin: Factor w/ 11930 levels "-","0.001","0.022",..: 395 331 351 364 172 358 285 447 285 209 ...
##  $ coef_dist       : num  0.012 0.098 0.07 0.055 0.066 0.095 0.15 0.097 0.101 0.115 ...
##  $ dist_avg_price  : Factor w/ 15997 levels "-","0.506","0.547",..: 220 158 365 225 336 352 495 511 326 342 ...
##  $ dist_sd_price   : Factor w/ 5858 levels "-","0","0.001",..: 112 90 97 121 79 130 70 93 74 93 ...
##  $ dist_min_price  : Factor w/ 21620 levels "-","0.3257","0.326",..: 20 115 194 250 543 208 518 1408 580 203 ...
##  $ dist_max_price  : Factor w/ 22576 levels "-","0.5992","0.7044",..: 221 381 596 783 350 1303 1052 1367 1068 1329 ...
##  $ coef_price      : Factor w/ 397 levels "-","0","0.001",..: 135 117 100 145 84 136 64 84 79 98 ...
##  $ year            : int  5 5 5 5 5 5 5 5 5 5 ...
##  $ month           : int  2004 2004 2004 2004 2004 2004 2004 2004 2004 2004 ...
df2 = df[,c("start_date" ,"fuel",'min_price'
            ,"max_price","avg_price","coef_dist","sd_price")]
df3= df2 %>% 
    group_by(start_date,fuel) %>%
    summarise_all("mean")
df3
## # A tibble: 4,263 x 7
## # Groups:   start_date [785]
##    start_date fuel         min_price max_price avg_price coef_dist sd_price
##    <fct>      <fct>            <dbl>     <dbl>     <dbl>     <dbl>    <dbl>
##  1 2004-05-09 Hydrous Eth…      1.10      1.70      1.34    0.0851   0.113 
##  2 2004-05-09 Regular Gas…      1.86      2.34      2.09    0.0384   0.0809
##  3 2004-05-09 LPG              28.5      36.4      32.6     0.0477   1.55  
##  4 2004-05-09 Natural Gas       1.10      1.15      1.13    0.0119   0.0131
##  5 2004-05-09 Diesel            1.32      1.61      1.44    0.0367   0.0529
##  6 2004-05-16 Hydrous Eth…      1.09      1.67      1.33    0.0863   0.114 
##  7 2004-05-16 Regular Gas…      1.86      2.32      2.09    0.0409   0.0863
##  8 2004-05-16 LPG              28.7      37.0      32.6     0.0501   1.64  
##  9 2004-05-16 Natural Gas       1.11      1.15      1.13    0.0111   0.0121
## 10 2004-05-16 Diesel            1.30      1.61      1.44    0.0377   0.0544
## # … with 4,253 more rows
unique(df3$fuel)
## [1] Hydrous Ethanol  Regular Gasoline LPG              Natural Gas     
## [5] Diesel           Diesel S10      
## 6 Levels: Hydrous Ethanol Regular Gasoline LPG Natural Gas ... Diesel S10
df3[df3$fuel=='Regular Gasoline',]
## # A tibble: 785 x 7
## # Groups:   start_date [785]
##    start_date fuel         min_price max_price avg_price coef_dist sd_price
##    <fct>      <fct>            <dbl>     <dbl>     <dbl>     <dbl>    <dbl>
##  1 2004-05-09 Regular Gas…      1.86      2.34      2.09    0.0384   0.0809
##  2 2004-05-16 Regular Gas…      1.86      2.32      2.09    0.0409   0.0863
##  3 2004-05-23 Regular Gas…      1.87      2.34      2.09    0.0416   0.0877
##  4 2004-05-30 Regular Gas…      1.87      2.33      2.10    0.0412   0.0873
##  5 2004-06-06 Regular Gas…      1.88      2.35      2.11    0.0394   0.0840
##  6 2004-06-13 Regular Gas…      1.88      2.40      2.14    0.045    0.0967
##  7 2004-06-20 Regular Gas…      1.97      2.44      2.22    0.0346   0.0774
##  8 2004-06-27 Regular Gas…      1.98      2.44      2.22    0.0350   0.0779
##  9 2004-07-04 Regular Gas…      1.99      2.46      2.22    0.0369   0.0817
## 10 2004-07-11 Regular Gas…      1.98      2.44      2.21    0.0371   0.0827
## # … with 775 more rows

data visulization

# df3=df3[df3$fuel!="Diesel S10",]
df3$start_date = as.Date( df3$start_date)

library(RColorBrewer)
col_prod = brewer.pal(9,"BrBG")

vis1 = ggplot() +
  geom_line(data = df3[df3$fuel=="Hydrous Ethanol",],aes(start_date, avg_price),col=col_prod[1]) +
  geom_ribbon(data = df3[df3$fuel=="Hydrous Ethanol",],aes(x = start_date, ymax = max_price, ymin = min_price), alpha = 0.6, fill = col_prod[1])+ theme(axis.title.x=element_blank(), axis.text.x=element_blank(), axis.title.y=element_blank())
vis2 =ggplot() +geom_line(data = df3[df3$fuel=="Regular Gasoline",],aes(start_date, avg_price),col=col_prod[2]) +
  geom_ribbon(data = df3[df3$fuel=="Regular Gasoline",], aes(x = start_date, ymax = max_price, ymin = min_price), alpha = 0.6, fill = col_prod[2])+ theme(axis.title.x=element_blank(), axis.text.x=element_blank(), axis.title.y=element_blank())
vis3 = ggplot() +   geom_line(data = df3[df3$fuel=="Natural Gas",],aes(start_date, avg_price),col=col_prod[7]) +
  geom_ribbon(data = df3[df3$fuel=="Natural Gas",], aes(x = start_date, ymax = max_price, ymin = min_price), alpha = 0.6, fill = col_prod[7])+ theme(axis.title.x=element_blank(), axis.text.x=element_blank(), axis.title.y=element_blank())
vis4 =  ggplot() +  geom_line(data = df3[df3$fuel=="Diesel",],aes(start_date, avg_price),col=col_prod[8]) +
  geom_ribbon(data = df3[df3$fuel=="Diesel",], aes(x = start_date, ymax = max_price, ymin = min_price), alpha = 0.6, fill = col_prod[8])  + theme(axis.title.x=element_blank(), axis.text.x=element_blank(), axis.title.y=element_blank())
vis5 =  ggplot() +  geom_line(data = df3[df3$fuel=="LPG",],aes(start_date, avg_price),col=col_prod[3]) +
  geom_ribbon(data = df3[df3$fuel=="LPG",], aes(x = start_date, ymax = max_price, ymin = min_price), alpha = 0.6, fill = col_prod[3])+ theme(axis.title.x=element_blank(), axis.text.x=element_blank(), axis.title.y=element_blank()) 
vis6 =  ggplot() +  geom_line(data = df3[df3$fuel=="Diesel S10",],aes(start_date, avg_price),col=col_prod[9]) +
  geom_ribbon(data = df3[df3$fuel=="Diesel S10",], aes(x = start_date, ymax = max_price, ymin = min_price), alpha = 0.6, fill = col_prod[9])+ theme(axis.title.x=element_blank(), axis.text.x=element_blank(), axis.title.y=element_blank())  

#scale_x_date(labels = lbls, breaks =brks) 
ggarrange(vis1, vis2,vis5,vis4,vis3,vis6, labels = c("Hydrous Ethanol",  "Regular Gasoline","LPG","Natural Gas","Diesel" ,"Diesel S10" ), ncol = 3, nrow = 2, font.label = list(size = 25, face = "bold"))

ggarrange(vis1, vis2,vis5,vis4,vis3, labels = c("Hydrous Ethanol",  "Regular Gasoline","LPG","Natural Gas","Diesel"  ), ncol = 3, nrow = 2, font.label = list(size = 25, face = "bold"))

# decompose
HE_ts=ts(df3[df3$fuel=="Hydrous Ethanol",]$avg_price,frequency=52)
RG_ts=ts(df3[df3$fuel=="Regular Gasoline",]$avg_price,frequency=52)
NG_ts=ts(df3[df3$fuel=="Natural Gas",]$avg_price,frequency=52)
LPG_ts=ts(df3[df3$fuel=="LPG",]$avg_price,frequency=52)
D_ts=ts(df3[df3$fuel=="Diesel",]$avg_price,frequency=52)

ts_list = c(HE_ts,RG_ts,NG_ts,LPG_ts,D_ts)
plot(decompose(HE_ts))

plot(decompose(RG_ts))

plot(decompose(NG_ts))

plot(decompose(LPG_ts))

plot(decompose(D_ts))

# decompose
HE_ts=ts(df3[df3$fuel=="Hydrous Ethanol",]$avg_price)
RG_ts=ts(df3[df3$fuel=="Regular Gasoline",]$avg_price)
NG_ts=ts(df3[df3$fuel=="Natural Gas",]$avg_price)
LPG_ts=ts(df3[df3$fuel=="LPG",]$avg_price)
D_ts=ts(df3[df3$fuel=="Diesel",]$avg_price)



# frequency analysis
sp=spectrum(HE_ts, log="no")

plot(sp,log='no',main="Periodogram for Hydrous Ethanol",xlim=c(0,0.5))
points(sp$freq[which(sp$spec==sort(sp$spec,decreasing = T)[1])],sort(sp$spec,decreasing = T)[1],col=2)
points(sp$freq[which(sp$spec==sort(sp$spec,decreasing = T)[2])],sort(sp$spec,decreasing = T)[2],col=2)
text(sp$freq[which(sp$spec==sort(sp$spec,decreasing = T)[2])]+0.03,sort(sp$spec,decreasing = T)[2],paste(sp$freq[which(sp$spec==sort(sp$spec,decreasing = T)[2])]))
text(sp$freq[which(sp$spec==sort(sp$spec,decreasing = T)[1])]+0.03,sort(sp$spec,decreasing = T)[1],paste(sp$freq[which(sp$spec==sort(sp$spec,decreasing = T)[1])]))

sp=spectrum(RG_ts, log="no")

plot(sp,log='no',main="Periodogram for Regular Gasoline",xlim=c(0,0.5))
points(sp$freq[which(sp$spec==sort(sp$spec,decreasing = T)[1])],sort(sp$spec,decreasing = T)[1],col=2)
points(sp$freq[which(sp$spec==sort(sp$spec,decreasing = T)[2])],sort(sp$spec,decreasing = T)[2],col=2)
text(sp$freq[which(sp$spec==sort(sp$spec,decreasing = T)[2])]+0.03,sort(sp$spec,decreasing = T)[2],paste(sp$freq[which(sp$spec==sort(sp$spec,decreasing = T)[2])]))
text(sp$freq[which(sp$spec==sort(sp$spec,decreasing = T)[1])]+0.03,sort(sp$spec,decreasing = T)[1],paste(sp$freq[which(sp$spec==sort(sp$spec,decreasing = T)[1])]))

sp=spectrum(NG_ts, log="no")

plot(sp,log='no',main="Periodogram for Natural Gas",xlim=c(0,0.5))
points(sp$freq[which(sp$spec==sort(sp$spec,decreasing = T)[1])],sort(sp$spec,decreasing = T)[1],col=2)
points(sp$freq[which(sp$spec==sort(sp$spec,decreasing = T)[3])],sort(sp$spec,decreasing = T)[3],col=2)
text(sp$freq[which(sp$spec==sort(sp$spec,decreasing = T)[3])]+0.03,sort(sp$spec,decreasing = T)[3],paste(sp$freq[which(sp$spec==sort(sp$spec,decreasing = T)[3])]))
text(sp$freq[which(sp$spec==sort(sp$spec,decreasing = T)[1])]+0.03,sort(sp$spec,decreasing = T)[1],paste(sp$freq[which(sp$spec==sort(sp$spec,decreasing = T)[1])]))

sp=spectrum(LPG_ts, log="no")

plot(sp,log='no',xlim=c(0,0.5),main="Periodogram for LPG")
points(sp$freq[which(sp$spec==sort(sp$spec,decreasing = T)[1])],sort(sp$spec,decreasing = T)[1],col=2)
points(sp$freq[which(sp$spec==sort(sp$spec,decreasing = T)[4])],sort(sp$spec,decreasing = T)[4],col=2)
text(sp$freq[which(sp$spec==sort(sp$spec,decreasing = T)[4])]+0.03,sort(sp$spec,decreasing = T)[4],paste(sp$freq[which(sp$spec==sort(sp$spec,decreasing = T)[2])]))
text(sp$freq[which(sp$spec==sort(sp$spec,decreasing = T)[1])]+0.03,sort(sp$spec,decreasing = T)[1],paste(sp$freq[which(sp$spec==sort(sp$spec,decreasing = T)[1])]))

sp=spectrum(D_ts, log="no")

plot(sp,log='no',xlim=c(0,0.5),main="Periodogram for Diesel")
points(sp$freq[which(sp$spec==sort(sp$spec,decreasing = T)[1])],sort(sp$spec,decreasing = T)[1],col=2)
points(sp$freq[which(sp$spec==sort(sp$spec,decreasing = T)[3])],sort(sp$spec,decreasing = T)[3],col=2)
text(sp$freq[which(sp$spec==sort(sp$spec,decreasing = T)[3])]+0.03,sort(sp$spec,decreasing = T)[3],paste(sp$freq[which(sp$spec==sort(sp$spec,decreasing = T)[3])]))
text(sp$freq[which(sp$spec==sort(sp$spec,decreasing = T)[1])]+0.03,sort(sp$spec,decreasing = T)[1],paste(sp$freq[which(sp$spec==sort(sp$spec,decreasing = T)[1])]))

# function for extract the data 
df4 = df3[df3$fuel=='Hydrous Ethanol',][,c('start_date','avg_price')]
df4=merge(df4,df3[df3$fuel=='Regular Gasoline',][,c('start_date','avg_price')],by='start_date')
colnames(df4)=c('start_date',"Hydrous Ethanol",  "Regular Gasoline")
df4=merge(df4,df3[df3$fuel=='LPG',][,c('start_date','avg_price')],by='start_date')
df4=merge(df4,df3[df3$fuel=='Natural Gas',][,c('start_date','avg_price')],by='start_date')
df4=merge(df4,df3[df3$fuel=='Diesel',][,c('start_date','avg_price')],by='start_date')

colnames(df4) = c('start_date',"Hydrous Ethanol",  "Regular Gasoline", "LPG","Natural Gas","Diesel" )
# which(df4$start_date == as.Date("2004-05-16"))
df4['group'] = c(rep(1,785-52),rep(2,52))
df4$start_date = as.Date( df4$start_date)
str(df4)
## 'data.frame':    785 obs. of  7 variables:
##  $ start_date      : Date, format: "2004-05-09" "2004-05-16" ...
##  $ Hydrous Ethanol : num  1.34 1.33 1.34 1.37 1.38 ...
##  $ Regular Gasoline: num  2.09 2.09 2.09 2.1 2.11 ...
##  $ LPG             : num  32.6 32.6 32.6 32.5 32.6 ...
##  $ Natural Gas     : num  1.13 1.13 1.13 1.13 1.14 ...
##  $ Diesel          : num  1.44 1.44 1.44 1.44 1.44 ...
##  $ group           : num  1 1 1 1 1 1 1 1 1 1 ...
# scale the data
df5 = data.frame( apply(df4[,2:6],2,scale))
df5['date'] = as.Date(df4[,1])
df5['group']=df4[,7]
# split df to two group, set last year data as the reference
df5_scaled = df5[df5$group==1,]
df5_scaled2 = df5[df5$group!=1,]
df5_scaled$group=NULL

# labels and breaks for X axis text
brks = df5_scaled$date[seq(1, length(df5_scaled$date),52)]
lbls = unique(lubridate::year(df5_scaled$date))

df5_scaled_melt <- reshape2::melt(df5_scaled, "date")
# plot
ggplot(df5_scaled_melt, aes(x=date)) + 
  geom_line(aes(y=value,col=variable)) + 
  labs(title="Scaled Average Gas Price", 
       y="Average Price", 
       color=NULL) +  # title 
  scale_x_date(labels = lbls, breaks =brks) + # change to monthly ticks and labels
scale_fill_brewer( palette = 3) +
     theme(legend.position="top")+theme_gray()

 # turn off minor grid

# stl(ts(df5_scaled, frequency=52),
#     s.window = "periodic") %>% plot

acf1=ggAcf(df5_scaled$Hydrous.Ethanol,200)
acf2=ggAcf(df5_scaled$Regular.Gasoline,200)
acf3=ggAcf(df5_scaled$LPG,200)
acf4=ggAcf(df5_scaled$Natural.Gas,200)
acf5=ggAcf(df5_scaled$Diesel,200)
ggarrange(acf1, acf2,acf3,acf4,acf5,
          ncol = 1, nrow = 5)

# rename the prepared data 
df5_diff = data.frame( apply(df5_scaled[,1:5],2,diff))
df5_diff['date'] = as.Date(df5_scaled[,1][-1])

df5_diff_melt = reshape2::melt(df5_diff, "date")
# plot
ggplot(df5_diff_melt, aes(x=date)) + 
  geom_line(aes(y=value,col=variable)) + 
  labs(title="Average gas price with first differencing", 
       y="Average Price", 
       color=NULL) +  # title 
  scale_x_date(labels = lbls, breaks =brks) + # change to monthly ticks and labels
scale_fill_brewer( palette = 3) +
     theme(legend.position="top")+theme_gray()

acf1=ggAcf(df5_diff$Hydrous.Ethanol,200)
acf2=ggAcf(df5_diff$Regular.Gasoline,200)
acf3=ggAcf(df5_diff$LPG,200)
acf4=ggAcf(df5_diff$Natural.Gas,200)
acf5=ggAcf(df5_diff$Diesel,200)




# rename the prepared data 
df5_diff2 =df5_diff
df5_diff2$Hydrous.Ethanol = c(diff(df5_diff2$Hydrous.Ethanol),NA)
df5_diff2$LPG = c(diff(df5_diff2$LPG),NA)



df5_diff_melt2 <- reshape2::melt(df5_diff2, "date")
# plot
ggplot(df5_diff_melt2, aes(x=date)) + 
  geom_line(aes(y=value,col=variable)) + 
  labs(title="Average gas price with second differencing.", 
       y="Average Price", 
       color=NULL) +  # title 
  scale_x_date(labels = lbls, breaks =brks) + # change to monthly ticks and labels
scale_fill_brewer( palette = 3) +
     theme(legend.position="top")+theme_gray()

acf1=ggAcf(df5_diff2$Hydrous.Ethanol,200)
acf2=ggAcf(df5_diff2$Regular.Gasoline,200)
acf3=ggAcf(df5_diff2$LPG,200)
acf4=ggAcf(df5_diff2$Natural.Gas,200)
acf5=ggAcf(df5_diff2$Diesel,200)
ggarrange(acf1, acf2,acf3,acf4,acf5,
          ncol = 1, nrow = 5)

VAR:product price as exogenous variables

https://www.r-econometrics.com/timeseries/varintro/

# df5_diff$date = NULL
# df5_diff = df5_diff[complete.cases(df5_diff2),]

var.fit2 = VAR(df5_scaled[,c(1:5)], 5, type = "trend", ic = "AIC",lag.max=3,season=80)
summary(var.fit2)
## 
## VAR Estimation Results:
## ========================= 
## Endogenous variables: Hydrous.Ethanol, Regular.Gasoline, LPG, Natural.Gas, Diesel 
## Deterministic variables: trend 
## Sample size: 730 
## Log Likelihood: 8374.04 
## Roots of the characteristic polynomial:
## 1.001 0.9926 0.9753 0.966 0.9204 0.8477 0.4354 0.4305 0.4305 0.4093 0.4093 0.3389 0.1978 0.1978 0.1978
## Call:
## VAR(y = df5_scaled[, c(1:5)], p = 5, type = "trend", season = 80L, 
##     lag.max = 3, ic = "AIC")
## 
## 
## Estimation results for equation Hydrous.Ethanol: 
## ================================================ 
## Hydrous.Ethanol = Hydrous.Ethanol.l1 + Regular.Gasoline.l1 + LPG.l1 + Natural.Gas.l1 + Diesel.l1 + Hydrous.Ethanol.l2 + Regular.Gasoline.l2 + LPG.l2 + Natural.Gas.l2 + Diesel.l2 + Hydrous.Ethanol.l3 + Regular.Gasoline.l3 + LPG.l3 + Natural.Gas.l3 + Diesel.l3 + trend + sd1 + sd2 + sd3 + sd4 + sd5 + sd6 + sd7 + sd8 + sd9 + sd10 + sd11 + sd12 + sd13 + sd14 + sd15 + sd16 + sd17 + sd18 + sd19 + sd20 + sd21 + sd22 + sd23 + sd24 + sd25 + sd26 + sd27 + sd28 + sd29 + sd30 + sd31 + sd32 + sd33 + sd34 + sd35 + sd36 + sd37 + sd38 + sd39 + sd40 + sd41 + sd42 + sd43 + sd44 + sd45 + sd46 + sd47 + sd48 + sd49 + sd50 + sd51 + sd52 + sd53 + sd54 + sd55 + sd56 + sd57 + sd58 + sd59 + sd60 + sd61 + sd62 + sd63 + sd64 + sd65 + sd66 + sd67 + sd68 + sd69 + sd70 + sd71 + sd72 + sd73 + sd74 + sd75 + sd76 + sd77 + sd78 + sd79 
## 
##                       Estimate Std. Error t value Pr(>|t|)    
## Hydrous.Ethanol.l1   1.488e+00  4.915e-02  30.265  < 2e-16 ***
## Regular.Gasoline.l1 -1.536e-01  4.503e-02  -3.411 0.000688 ***
## LPG.l1              -3.950e-02  4.200e-02  -0.940 0.347421    
## Natural.Gas.l1       1.339e-02  2.435e-02   0.550 0.582686    
## Diesel.l1            4.145e-02  3.566e-02   1.162 0.245515    
## Hydrous.Ethanol.l2  -2.027e-01  8.769e-02  -2.311 0.021137 *  
## Regular.Gasoline.l2 -2.199e-02  7.187e-02  -0.306 0.759725    
## LPG.l2               1.202e-01  6.569e-02   1.829 0.067840 .  
## Natural.Gas.l2      -1.707e-02  2.333e-02  -0.732 0.464481    
## Diesel.l2            3.740e-02  5.508e-02   0.679 0.497340    
## Hydrous.Ethanol.l3  -2.992e-01  4.899e-02  -6.107 1.77e-09 ***
## Regular.Gasoline.l3  1.593e-01  4.581e-02   3.476 0.000543 ***
## LPG.l3              -5.548e-02  4.269e-02  -1.300 0.194206    
## Natural.Gas.l3      -6.289e-03  2.442e-02  -0.258 0.796849    
## Diesel.l3           -6.424e-02  3.636e-02  -1.767 0.077755 .  
## trend                5.371e-06  2.567e-06   2.093 0.036765 *  
## sd1                 -5.663e-03  1.098e-02  -0.516 0.606123    
## sd2                 -4.813e-03  1.098e-02  -0.438 0.661180    
## sd3                  4.617e-03  1.097e-02   0.421 0.674011    
## sd4                 -4.662e-03  1.070e-02  -0.436 0.663152    
## sd5                  2.378e-03  1.069e-02   0.222 0.824102    
## sd6                  3.366e-03  1.068e-02   0.315 0.752806    
## sd7                 -1.871e-03  1.071e-02  -0.175 0.861370    
## sd8                 -3.748e-03  1.069e-02  -0.350 0.726129    
## sd9                 -3.725e-03  1.070e-02  -0.348 0.727833    
## sd10                -2.112e-03  1.073e-02  -0.197 0.844023    
## sd11                 1.195e-03  1.088e-02   0.110 0.912583    
## sd12                 1.115e-02  1.086e-02   1.027 0.304978    
## sd13                -2.605e-03  1.071e-02  -0.243 0.807878    
## sd14                 1.349e-02  1.098e-02   1.229 0.219580    
## sd15                -5.156e-03  1.099e-02  -0.469 0.639168    
## sd16                 4.701e-03  1.099e-02   0.428 0.668869    
## sd17                -5.005e-03  1.100e-02  -0.455 0.649222    
## sd18                -6.416e-03  1.096e-02  -0.585 0.558554    
## sd19                -1.223e-03  1.099e-02  -0.111 0.911404    
## sd20                -1.983e-03  1.098e-02  -0.181 0.856785    
## sd21                -5.403e-03  1.096e-02  -0.493 0.622285    
## sd22                 1.762e-03  1.096e-02   0.161 0.872287    
## sd23                -6.670e-03  1.098e-02  -0.607 0.543822    
## sd24                 2.742e-03  1.096e-02   0.250 0.802603    
## sd25                -6.411e-03  1.096e-02  -0.585 0.558930    
## sd26                -1.020e-02  1.098e-02  -0.929 0.353290    
## sd27                 1.129e-02  1.100e-02   1.026 0.305085    
## sd28                 4.002e-05  1.119e-02   0.004 0.997146    
## sd29                 1.891e-03  1.119e-02   0.169 0.865811    
## sd30                 5.753e-04  1.106e-02   0.052 0.958531    
## sd31                 1.245e-02  1.102e-02   1.130 0.258864    
## sd32                -3.370e-05  1.102e-02  -0.003 0.997560    
## sd33                -4.496e-03  1.100e-02  -0.409 0.682781    
## sd34                -9.581e-04  1.098e-02  -0.087 0.930504    
## sd35                 8.667e-03  1.097e-02   0.790 0.429689    
## sd36                 9.560e-03  1.102e-02   0.868 0.385881    
## sd37                 9.529e-03  1.102e-02   0.865 0.387584    
## sd38                -1.586e-02  1.101e-02  -1.441 0.150154    
## sd39                 1.250e-02  1.104e-02   1.132 0.257929    
## sd40                -1.931e-03  1.102e-02  -0.175 0.860971    
## sd41                -1.569e-02  1.099e-02  -1.428 0.153857    
## sd42                -4.275e-03  1.101e-02  -0.388 0.697936    
## sd43                -2.027e-02  1.101e-02  -1.841 0.066056 .  
## sd44                -6.159e-03  1.099e-02  -0.560 0.575478    
## sd45                 2.550e-02  1.101e-02   2.316 0.020894 *  
## sd46                -6.180e-04  1.113e-02  -0.056 0.955727    
## sd47                 2.287e-03  1.105e-02   0.207 0.836185    
## sd48                -1.901e-03  1.100e-02  -0.173 0.862865    
## sd49                -1.346e-03  1.104e-02  -0.122 0.903038    
## sd50                 5.245e-03  1.099e-02   0.477 0.633270    
## sd51                -5.579e-04  1.098e-02  -0.051 0.959503    
## sd52                 7.407e-03  1.098e-02   0.675 0.500180    
## sd53                 8.370e-03  1.099e-02   0.762 0.446465    
## sd54                -1.780e-03  1.102e-02  -0.162 0.871663    
## sd55                -1.053e-03  1.099e-02  -0.096 0.923721    
## sd56                 1.254e-02  1.100e-02   1.139 0.254934    
## sd57                -2.833e-03  1.101e-02  -0.257 0.797009    
## sd58                -4.333e-03  1.102e-02  -0.393 0.694255    
## sd59                -1.746e-02  1.105e-02  -1.580 0.114567    
## sd60                -7.120e-04  1.101e-02  -0.065 0.948464    
## sd61                -1.207e-02  1.101e-02  -1.097 0.273250    
## sd62                -3.289e-03  1.100e-02  -0.299 0.765034    
## sd63                -6.604e-03  1.099e-02  -0.601 0.548122    
## sd64                -1.761e-04  1.101e-02  -0.016 0.987235    
## sd65                -8.652e-03  1.099e-02  -0.787 0.431329    
## sd66                 3.965e-03  1.098e-02   0.361 0.718198    
## sd67                -5.637e-03  1.100e-02  -0.513 0.608464    
## sd68                 4.250e-03  1.097e-02   0.387 0.698698    
## sd69                 5.732e-03  1.099e-02   0.521 0.602312    
## sd70                -7.180e-04  1.101e-02  -0.065 0.948015    
## sd71                -2.529e-03  1.099e-02  -0.230 0.818150    
## sd72                 4.461e-04  1.097e-02   0.041 0.967578    
## sd73                 9.124e-03  1.096e-02   0.832 0.405462    
## sd74                -1.113e-02  1.097e-02  -1.015 0.310647    
## sd75                -9.452e-03  1.098e-02  -0.861 0.389551    
## sd76                -2.183e-03  1.096e-02  -0.199 0.842192    
## sd77                 5.641e-03  1.096e-02   0.515 0.607062    
## sd78                 2.494e-03  1.104e-02   0.226 0.821289    
## sd79                -2.980e-03  1.099e-02  -0.271 0.786369    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## 
## Residual standard error: 0.0232 on 635 degrees of freedom
## Multiple R-Squared: 0.9994,  Adjusted R-squared: 0.9993 
## F-statistic: 1.161e+04 on 95 and 635 DF,  p-value: < 2.2e-16 
## 
## 
## Estimation results for equation Regular.Gasoline: 
## ================================================= 
## Regular.Gasoline = Hydrous.Ethanol.l1 + Regular.Gasoline.l1 + LPG.l1 + Natural.Gas.l1 + Diesel.l1 + Hydrous.Ethanol.l2 + Regular.Gasoline.l2 + LPG.l2 + Natural.Gas.l2 + Diesel.l2 + Hydrous.Ethanol.l3 + Regular.Gasoline.l3 + LPG.l3 + Natural.Gas.l3 + Diesel.l3 + trend + sd1 + sd2 + sd3 + sd4 + sd5 + sd6 + sd7 + sd8 + sd9 + sd10 + sd11 + sd12 + sd13 + sd14 + sd15 + sd16 + sd17 + sd18 + sd19 + sd20 + sd21 + sd22 + sd23 + sd24 + sd25 + sd26 + sd27 + sd28 + sd29 + sd30 + sd31 + sd32 + sd33 + sd34 + sd35 + sd36 + sd37 + sd38 + sd39 + sd40 + sd41 + sd42 + sd43 + sd44 + sd45 + sd46 + sd47 + sd48 + sd49 + sd50 + sd51 + sd52 + sd53 + sd54 + sd55 + sd56 + sd57 + sd58 + sd59 + sd60 + sd61 + sd62 + sd63 + sd64 + sd65 + sd66 + sd67 + sd68 + sd69 + sd70 + sd71 + sd72 + sd73 + sd74 + sd75 + sd76 + sd77 + sd78 + sd79 
## 
##                       Estimate Std. Error t value Pr(>|t|)    
## Hydrous.Ethanol.l1   7.966e-02  6.865e-02   1.160  0.24635    
## Regular.Gasoline.l1  1.090e+00  6.289e-02  17.329  < 2e-16 ***
## LPG.l1              -3.613e-02  5.867e-02  -0.616  0.53817    
## Natural.Gas.l1       8.789e-02  3.400e-02   2.585  0.00997 ** 
## Diesel.l1            1.156e-01  4.980e-02   2.321  0.02061 *  
## Hydrous.Ethanol.l2   2.111e-02  1.225e-01   0.172  0.86319    
## Regular.Gasoline.l2 -2.245e-01  1.004e-01  -2.237  0.02564 *  
## LPG.l2               6.516e-02  9.174e-02   0.710  0.47779    
## Natural.Gas.l2      -1.063e-01  3.258e-02  -3.261  0.00117 ** 
## Diesel.l2           -6.657e-02  7.693e-02  -0.865  0.38719    
## Hydrous.Ethanol.l3  -1.060e-01  6.842e-02  -1.549  0.12181    
## Regular.Gasoline.l3  9.455e-02  6.399e-02   1.478  0.13999    
## LPG.l3               9.419e-03  5.963e-02   0.158  0.87454    
## Natural.Gas.l3       1.579e-03  3.410e-02   0.046  0.96308    
## Diesel.l3           -2.651e-02  5.078e-02  -0.522  0.60183    
## trend                7.282e-06  3.585e-06   2.031  0.04262 *  
## sd1                 -3.700e-03  1.533e-02  -0.241  0.80939    
## sd2                 -5.974e-03  1.533e-02  -0.390  0.69691    
## sd3                  6.110e-04  1.532e-02   0.040  0.96820    
## sd4                 -4.534e-03  1.494e-02  -0.303  0.76167    
## sd5                 -2.712e-03  1.494e-02  -0.182  0.85598    
## sd6                 -7.188e-04  1.492e-02  -0.048  0.96159    
## sd7                  6.621e-03  1.496e-02   0.443  0.65817    
## sd8                  7.235e-03  1.494e-02   0.484  0.62830    
## sd9                  7.858e-03  1.494e-02   0.526  0.59915    
## sd10                -9.638e-03  1.499e-02  -0.643  0.52042    
## sd11                -3.658e-03  1.520e-02  -0.241  0.80985    
## sd12                 5.087e-03  1.517e-02   0.335  0.73756    
## sd13                -2.969e-04  1.495e-02  -0.020  0.98417    
## sd14                 2.455e-03  1.534e-02   0.160  0.87288    
## sd15                -6.485e-03  1.535e-02  -0.422  0.67285    
## sd16                 1.017e-02  1.534e-02   0.663  0.50759    
## sd17                -8.794e-03  1.536e-02  -0.572  0.56720    
## sd18                -6.935e-03  1.531e-02  -0.453  0.65072    
## sd19                 3.134e-04  1.535e-02   0.020  0.98371    
## sd20                -5.197e-03  1.534e-02  -0.339  0.73481    
## sd21                -6.704e-03  1.531e-02  -0.438  0.66166    
## sd22                -3.977e-04  1.530e-02  -0.026  0.97927    
## sd23                -1.598e-03  1.534e-02  -0.104  0.91705    
## sd24                 1.465e-03  1.531e-02   0.096  0.92383    
## sd25                -1.481e-02  1.531e-02  -0.967  0.33367    
## sd26                -8.600e-03  1.533e-02  -0.561  0.57510    
## sd27                 2.187e-03  1.537e-02   0.142  0.88688    
## sd28                -3.488e-03  1.562e-02  -0.223  0.82339    
## sd29                -6.266e-03  1.562e-02  -0.401  0.68847    
## sd30                 1.279e-02  1.545e-02   0.828  0.40808    
## sd31                 1.664e-02  1.539e-02   1.082  0.27988    
## sd32                -1.081e-02  1.539e-02  -0.703  0.48260    
## sd33                -1.984e-03  1.536e-02  -0.129  0.89725    
## sd34                -5.692e-03  1.534e-02  -0.371  0.71071    
## sd35                -1.007e-02  1.532e-02  -0.658  0.51104    
## sd36                 7.943e-03  1.539e-02   0.516  0.60590    
## sd37                 1.039e-03  1.539e-02   0.067  0.94621    
## sd38                -6.514e-03  1.538e-02  -0.424  0.67198    
## sd39                 6.357e-03  1.541e-02   0.412  0.68016    
## sd40                -4.748e-03  1.539e-02  -0.308  0.75786    
## sd41                -1.389e-02  1.535e-02  -0.905  0.36606    
## sd42                -1.312e-03  1.538e-02  -0.085  0.93205    
## sd43                -1.645e-02  1.537e-02  -1.070  0.28493    
## sd44                -6.438e-03  1.535e-02  -0.419  0.67507    
## sd45                 4.856e-02  1.538e-02   3.157  0.00167 ** 
## sd46                -1.089e-02  1.554e-02  -0.701  0.48378    
## sd47                -5.565e-03  1.544e-02  -0.360  0.71860    
## sd48                -5.495e-03  1.536e-02  -0.358  0.72071    
## sd49                 8.408e-05  1.542e-02   0.005  0.99565    
## sd50                -2.535e-03  1.535e-02  -0.165  0.86886    
## sd51                 9.752e-03  1.534e-02   0.636  0.52513    
## sd52                -2.257e-03  1.533e-02  -0.147  0.88301    
## sd53                 2.285e-02  1.535e-02   1.489  0.13693    
## sd54                -7.066e-03  1.539e-02  -0.459  0.64621    
## sd55                -9.801e-03  1.536e-02  -0.638  0.52355    
## sd56                 4.402e-03  1.537e-02   0.286  0.77465    
## sd57                -9.213e-03  1.538e-02  -0.599  0.54925    
## sd58                -7.681e-03  1.539e-02  -0.499  0.61783    
## sd59                -1.124e-02  1.543e-02  -0.729  0.46656    
## sd60                 5.410e-04  1.538e-02   0.035  0.97195    
## sd61                -2.746e-03  1.538e-02  -0.179  0.85833    
## sd62                 3.031e-03  1.536e-02   0.197  0.84366    
## sd63                -1.812e-03  1.535e-02  -0.118  0.90604    
## sd64                -1.160e-03  1.537e-02  -0.075  0.93984    
## sd65                -3.053e-03  1.535e-02  -0.199  0.84237    
## sd66                -5.380e-05  1.534e-02  -0.004  0.99720    
## sd67                -6.839e-03  1.536e-02  -0.445  0.65634    
## sd68                 5.547e-03  1.533e-02   0.362  0.71752    
## sd69                 2.647e-02  1.536e-02   1.724  0.08521 .  
## sd70                -1.133e-02  1.537e-02  -0.737  0.46157    
## sd71                -3.493e-03  1.535e-02  -0.227  0.82012    
## sd72                 1.415e-03  1.532e-02   0.092  0.92642    
## sd73                -7.725e-03  1.531e-02  -0.505  0.61399    
## sd74                -1.105e-02  1.533e-02  -0.721  0.47128    
## sd75                -6.187e-03  1.533e-02  -0.404  0.68670    
## sd76                -8.186e-03  1.531e-02  -0.535  0.59295    
## sd77                 3.395e-02  1.531e-02   2.217  0.02695 *  
## sd78                -2.091e-03  1.541e-02  -0.136  0.89211    
## sd79                -6.597e-03  1.535e-02  -0.430  0.66752    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## 
## Residual standard error: 0.0324 on 635 degrees of freedom
## Multiple R-Squared: 0.9987,  Adjusted R-squared: 0.9985 
## F-statistic:  5037 on 95 and 635 DF,  p-value: < 2.2e-16 
## 
## 
## Estimation results for equation LPG: 
## ==================================== 
## LPG = Hydrous.Ethanol.l1 + Regular.Gasoline.l1 + LPG.l1 + Natural.Gas.l1 + Diesel.l1 + Hydrous.Ethanol.l2 + Regular.Gasoline.l2 + LPG.l2 + Natural.Gas.l2 + Diesel.l2 + Hydrous.Ethanol.l3 + Regular.Gasoline.l3 + LPG.l3 + Natural.Gas.l3 + Diesel.l3 + trend + sd1 + sd2 + sd3 + sd4 + sd5 + sd6 + sd7 + sd8 + sd9 + sd10 + sd11 + sd12 + sd13 + sd14 + sd15 + sd16 + sd17 + sd18 + sd19 + sd20 + sd21 + sd22 + sd23 + sd24 + sd25 + sd26 + sd27 + sd28 + sd29 + sd30 + sd31 + sd32 + sd33 + sd34 + sd35 + sd36 + sd37 + sd38 + sd39 + sd40 + sd41 + sd42 + sd43 + sd44 + sd45 + sd46 + sd47 + sd48 + sd49 + sd50 + sd51 + sd52 + sd53 + sd54 + sd55 + sd56 + sd57 + sd58 + sd59 + sd60 + sd61 + sd62 + sd63 + sd64 + sd65 + sd66 + sd67 + sd68 + sd69 + sd70 + sd71 + sd72 + sd73 + sd74 + sd75 + sd76 + sd77 + sd78 + sd79 
## 
##                       Estimate Std. Error t value Pr(>|t|)    
## Hydrous.Ethanol.l1   3.146e-02  4.630e-02   0.679 0.497152    
## Regular.Gasoline.l1 -3.694e-02  4.242e-02  -0.871 0.384207    
## LPG.l1               1.208e+00  3.957e-02  30.530  < 2e-16 ***
## Natural.Gas.l1       8.371e-04  2.293e-02   0.037 0.970895    
## Diesel.l1            7.475e-02  3.359e-02   2.225 0.026413 *  
## Hydrous.Ethanol.l2  -1.103e-01  8.260e-02  -1.336 0.182184    
## Regular.Gasoline.l2  4.717e-02  6.770e-02   0.697 0.486165    
## LPG.l2              -1.503e-01  6.188e-02  -2.429 0.015415 *  
## Natural.Gas.l2       4.959e-02  2.197e-02   2.257 0.024347 *  
## Diesel.l2           -5.493e-02  5.188e-02  -1.059 0.290125    
## Hydrous.Ethanol.l3   7.062e-02  4.614e-02   1.530 0.126411    
## Regular.Gasoline.l3  5.872e-04  4.316e-02   0.014 0.989148    
## LPG.l3              -6.261e-02  4.021e-02  -1.557 0.120020    
## Natural.Gas.l3      -5.203e-02  2.300e-02  -2.262 0.024044 *  
## Diesel.l3           -1.430e-02  3.425e-02  -0.417 0.676561    
## trend                9.235e-06  2.418e-06   3.820 0.000147 ***
## sd1                 -2.264e-02  1.034e-02  -2.190 0.028911 *  
## sd2                 -9.164e-03  1.034e-02  -0.886 0.375792    
## sd3                 -1.412e-02  1.033e-02  -1.367 0.172211    
## sd4                 -1.231e-02  1.008e-02  -1.221 0.222381    
## sd5                 -9.225e-03  1.007e-02  -0.916 0.360142    
## sd6                 -1.710e-02  1.006e-02  -1.700 0.089675 .  
## sd7                 -1.482e-02  1.009e-02  -1.469 0.142265    
## sd8                 -1.102e-02  1.007e-02  -1.094 0.274438    
## sd9                 -6.120e-03  1.008e-02  -0.607 0.543899    
## sd10                -1.702e-02  1.011e-02  -1.684 0.092661 .  
## sd11                -1.053e-02  1.025e-02  -1.027 0.304592    
## sd12                -1.516e-02  1.023e-02  -1.481 0.139115    
## sd13                -1.463e-02  1.009e-02  -1.451 0.147361    
## sd14                -1.169e-02  1.034e-02  -1.130 0.259032    
## sd15                -1.011e-02  1.035e-02  -0.976 0.329399    
## sd16                -1.326e-02  1.035e-02  -1.281 0.200573    
## sd17                -1.354e-02  1.036e-02  -1.307 0.191625    
## sd18                -1.372e-02  1.033e-02  -1.328 0.184501    
## sd19                -1.657e-02  1.035e-02  -1.601 0.109954    
## sd20                -9.620e-03  1.034e-02  -0.930 0.352739    
## sd21                -1.408e-02  1.033e-02  -1.363 0.173212    
## sd22                -1.433e-02  1.032e-02  -1.389 0.165417    
## sd23                -1.581e-02  1.034e-02  -1.529 0.126807    
## sd24                -1.059e-02  1.033e-02  -1.026 0.305469    
## sd25                -1.568e-02  1.033e-02  -1.518 0.129449    
## sd26                -8.989e-03  1.034e-02  -0.869 0.385068    
## sd27                 3.793e-02  1.036e-02   3.659 0.000274 ***
## sd28                -4.838e-03  1.054e-02  -0.459 0.646244    
## sd29                -1.452e-02  1.054e-02  -1.378 0.168700    
## sd30                -1.569e-02  1.042e-02  -1.506 0.132531    
## sd31                -1.223e-02  1.038e-02  -1.179 0.239017    
## sd32                -1.121e-02  1.038e-02  -1.080 0.280367    
## sd33                -1.281e-02  1.036e-02  -1.236 0.216753    
## sd34                -1.338e-02  1.034e-02  -1.293 0.196345    
## sd35                -8.810e-03  1.033e-02  -0.853 0.394132    
## sd36                -8.323e-03  1.038e-02  -0.802 0.422875    
## sd37                -1.508e-02  1.038e-02  -1.452 0.146963    
## sd38                -1.097e-02  1.037e-02  -1.058 0.290536    
## sd39                -1.050e-02  1.039e-02  -1.010 0.312966    
## sd40                -8.326e-03  1.038e-02  -0.802 0.422914    
## sd41                -1.569e-02  1.035e-02  -1.516 0.130090    
## sd42                -1.097e-02  1.037e-02  -1.058 0.290682    
## sd43                -1.359e-02  1.037e-02  -1.311 0.190429    
## sd44                -9.403e-03  1.035e-02  -0.908 0.364139    
## sd45                -1.639e-02  1.037e-02  -1.580 0.114583    
## sd46                -1.964e-02  1.048e-02  -1.874 0.061374 .  
## sd47                -1.038e-02  1.041e-02  -0.996 0.319412    
## sd48                -1.225e-02  1.036e-02  -1.182 0.237590    
## sd49                -1.791e-02  1.040e-02  -1.722 0.085628 .  
## sd50                -1.048e-02  1.035e-02  -1.013 0.311546    
## sd51                -1.347e-02  1.034e-02  -1.302 0.193462    
## sd52                 1.314e-03  1.034e-02   0.127 0.898905    
## sd53                -1.480e-02  1.035e-02  -1.430 0.153110    
## sd54                 4.667e-03  1.038e-02   0.450 0.653001    
## sd55                -1.720e-02  1.036e-02  -1.661 0.097252 .  
## sd56                -8.244e-03  1.037e-02  -0.795 0.426725    
## sd57                 9.739e-03  1.037e-02   0.939 0.348003    
## sd58                -6.085e-03  1.038e-02  -0.586 0.557830    
## sd59                -1.993e-02  1.041e-02  -1.914 0.056022 .  
## sd60                -4.765e-03  1.037e-02  -0.459 0.646093    
## sd61                -1.509e-02  1.037e-02  -1.455 0.146114    
## sd62                -8.933e-03  1.036e-02  -0.862 0.388950    
## sd63                -1.060e-02  1.035e-02  -1.023 0.306467    
## sd64                -1.278e-02  1.037e-02  -1.233 0.218138    
## sd65                -1.098e-02  1.035e-02  -1.061 0.289078    
## sd66                -6.616e-03  1.034e-02  -0.640 0.522702    
## sd67                -1.849e-02  1.036e-02  -1.785 0.074724 .  
## sd68                -1.355e-02  1.034e-02  -1.310 0.190553    
## sd69                -1.707e-02  1.036e-02  -1.648 0.099801 .  
## sd70                -1.189e-02  1.037e-02  -1.147 0.251837    
## sd71                -1.642e-02  1.036e-02  -1.586 0.113236    
## sd72                -1.745e-02  1.033e-02  -1.689 0.091763 .  
## sd73                -1.557e-02  1.032e-02  -1.508 0.131964    
## sd74                -1.997e-02  1.034e-02  -1.932 0.053847 .  
## sd75                -1.577e-02  1.034e-02  -1.525 0.127866    
## sd76                -1.541e-02  1.032e-02  -1.493 0.135958    
## sd77                -1.269e-02  1.033e-02  -1.229 0.219440    
## sd78                -1.472e-02  1.039e-02  -1.416 0.157339    
## sd79                -1.258e-02  1.035e-02  -1.215 0.224775    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## 
## Residual standard error: 0.02185 on 635 degrees of freedom
## Multiple R-Squared: 0.9994,  Adjusted R-squared: 0.9993 
## F-statistic: 1.162e+04 on 95 and 635 DF,  p-value: < 2.2e-16 
## 
## 
## Estimation results for equation Natural.Gas: 
## ============================================ 
## Natural.Gas = Hydrous.Ethanol.l1 + Regular.Gasoline.l1 + LPG.l1 + Natural.Gas.l1 + Diesel.l1 + Hydrous.Ethanol.l2 + Regular.Gasoline.l2 + LPG.l2 + Natural.Gas.l2 + Diesel.l2 + Hydrous.Ethanol.l3 + Regular.Gasoline.l3 + LPG.l3 + Natural.Gas.l3 + Diesel.l3 + trend + sd1 + sd2 + sd3 + sd4 + sd5 + sd6 + sd7 + sd8 + sd9 + sd10 + sd11 + sd12 + sd13 + sd14 + sd15 + sd16 + sd17 + sd18 + sd19 + sd20 + sd21 + sd22 + sd23 + sd24 + sd25 + sd26 + sd27 + sd28 + sd29 + sd30 + sd31 + sd32 + sd33 + sd34 + sd35 + sd36 + sd37 + sd38 + sd39 + sd40 + sd41 + sd42 + sd43 + sd44 + sd45 + sd46 + sd47 + sd48 + sd49 + sd50 + sd51 + sd52 + sd53 + sd54 + sd55 + sd56 + sd57 + sd58 + sd59 + sd60 + sd61 + sd62 + sd63 + sd64 + sd65 + sd66 + sd67 + sd68 + sd69 + sd70 + sd71 + sd72 + sd73 + sd74 + sd75 + sd76 + sd77 + sd78 + sd79 
## 
##                       Estimate Std. Error t value Pr(>|t|)    
## Hydrous.Ethanol.l1   9.559e-03  7.947e-02   0.120 0.904290    
## Regular.Gasoline.l1  5.462e-02  7.280e-02   0.750 0.453388    
## LPG.l1               1.131e-01  6.791e-02   1.665 0.096441 .  
## Natural.Gas.l1       3.559e-01  3.936e-02   9.041  < 2e-16 ***
## Diesel.l1            1.951e-02  5.765e-02   0.338 0.735190    
## Hydrous.Ethanol.l2  -5.436e-03  1.418e-01  -0.038 0.969423    
## Regular.Gasoline.l2 -1.207e-01  1.162e-01  -1.039 0.299342    
## LPG.l2              -1.622e-01  1.062e-01  -1.527 0.127154    
## Natural.Gas.l2       4.818e-01  3.771e-02  12.776  < 2e-16 ***
## Diesel.l2            1.684e-01  8.905e-02   1.891 0.059055 .  
## Hydrous.Ethanol.l3  -1.137e-02  7.920e-02  -0.144 0.885850    
## Regular.Gasoline.l3  6.973e-02  7.407e-02   0.941 0.346858    
## LPG.l3               5.455e-02  6.902e-02   0.790 0.429635    
## Natural.Gas.l3       1.346e-01  3.948e-02   3.408 0.000695 ***
## Diesel.l3           -1.648e-01  5.878e-02  -2.803 0.005223 ** 
## trend                1.622e-05  4.149e-06   3.909 0.000103 ***
## sd1                 -1.305e-02  1.775e-02  -0.735 0.462333    
## sd2                 -2.275e-02  1.774e-02  -1.282 0.200272    
## sd3                 -1.798e-02  1.774e-02  -1.014 0.311000    
## sd4                 -1.776e-02  1.730e-02  -1.027 0.304944    
## sd5                  1.250e-02  1.729e-02   0.723 0.469943    
## sd6                 -9.481e-03  1.727e-02  -0.549 0.583187    
## sd7                 -9.261e-03  1.731e-02  -0.535 0.592910    
## sd8                 -9.678e-03  1.729e-02  -0.560 0.575867    
## sd9                 -1.679e-02  1.730e-02  -0.971 0.331997    
## sd10                -9.205e-03  1.735e-02  -0.531 0.595892    
## sd11                -1.927e-02  1.759e-02  -1.096 0.273640    
## sd12                 7.343e-03  1.756e-02   0.418 0.676046    
## sd13                -1.225e-02  1.731e-02  -0.708 0.479404    
## sd14                -6.954e-03  1.775e-02  -0.392 0.695394    
## sd15                -1.480e-02  1.777e-02  -0.833 0.405358    
## sd16                -2.243e-02  1.776e-02  -1.263 0.207143    
## sd17                -5.853e-03  1.778e-02  -0.329 0.742138    
## sd18                -2.830e-02  1.772e-02  -1.597 0.110754    
## sd19                -1.816e-02  1.777e-02  -1.022 0.307011    
## sd20                -1.016e-02  1.775e-02  -0.572 0.567373    
## sd21                 6.967e-04  1.772e-02   0.039 0.968655    
## sd22                -1.283e-02  1.771e-02  -0.724 0.469102    
## sd23                -3.863e-03  1.775e-02  -0.218 0.827818    
## sd24                 2.138e-03  1.773e-02   0.121 0.904022    
## sd25                -1.755e-02  1.772e-02  -0.990 0.322521    
## sd26                -3.039e-02  1.775e-02  -1.712 0.087378 .  
## sd27                -1.405e-03  1.779e-02  -0.079 0.937080    
## sd28                 1.253e-03  1.808e-02   0.069 0.944773    
## sd29                -3.010e-02  1.808e-02  -1.664 0.096530 .  
## sd30                -9.428e-03  1.788e-02  -0.527 0.598149    
## sd31                 8.248e-03  1.781e-02   0.463 0.643533    
## sd32                -8.909e-03  1.781e-02  -0.500 0.617135    
## sd33                -1.503e-02  1.778e-02  -0.846 0.398044    
## sd34                 1.765e-02  1.775e-02   0.994 0.320615    
## sd35                -1.180e-02  1.773e-02  -0.665 0.506001    
## sd36                -1.195e-02  1.781e-02  -0.671 0.502544    
## sd37                 2.442e-03  1.782e-02   0.137 0.891016    
## sd38                -1.969e-02  1.780e-02  -1.106 0.269042    
## sd39                -1.190e-02  1.784e-02  -0.667 0.505111    
## sd40                 1.691e-02  1.782e-02   0.949 0.342846    
## sd41                -1.316e-02  1.777e-02  -0.741 0.459196    
## sd42                -1.816e-02  1.780e-02  -1.020 0.307883    
## sd43                 4.130e-03  1.779e-02   0.232 0.816547    
## sd44                 8.353e-03  1.777e-02   0.470 0.638467    
## sd45                 4.277e-03  1.780e-02   0.240 0.810254    
## sd46                -1.702e-02  1.799e-02  -0.946 0.344320    
## sd47                 3.647e-04  1.787e-02   0.020 0.983722    
## sd48                 3.180e-03  1.779e-02   0.179 0.858134    
## sd49                -2.592e-02  1.785e-02  -1.452 0.146997    
## sd50                -2.233e-02  1.776e-02  -1.257 0.209259    
## sd51                -8.026e-03  1.775e-02  -0.452 0.651380    
## sd52                -2.453e-03  1.775e-02  -0.138 0.890117    
## sd53                -4.399e-03  1.776e-02  -0.248 0.804476    
## sd54                 1.984e-03  1.781e-02   0.111 0.911327    
## sd55                -1.640e-02  1.777e-02  -0.923 0.356593    
## sd56                -1.797e-02  1.779e-02  -1.010 0.312921    
## sd57                -5.023e-03  1.780e-02  -0.282 0.777855    
## sd58                -2.103e-02  1.781e-02  -1.181 0.238116    
## sd59                -1.583e-02  1.786e-02  -0.886 0.376001    
## sd60                -2.031e-02  1.780e-02  -1.141 0.254375    
## sd61                -1.577e-02  1.780e-02  -0.886 0.375986    
## sd62                 2.951e-03  1.778e-02   0.166 0.868234    
## sd63                -2.118e-02  1.777e-02  -1.192 0.233774    
## sd64                -8.171e-03  1.779e-02  -0.459 0.646196    
## sd65                -1.067e-02  1.776e-02  -0.601 0.548206    
## sd66                -1.647e-02  1.775e-02  -0.927 0.354078    
## sd67                -1.181e-02  1.778e-02  -0.664 0.506972    
## sd68                -1.741e-02  1.774e-02  -0.981 0.326767    
## sd69                 4.657e-03  1.777e-02   0.262 0.793404    
## sd70                -3.677e-03  1.780e-02  -0.207 0.836348    
## sd71                -2.079e-02  1.777e-02  -1.170 0.242597    
## sd72                -8.160e-03  1.774e-02  -0.460 0.645597    
## sd73                -1.023e-02  1.772e-02  -0.578 0.563792    
## sd74                -1.166e-02  1.774e-02  -0.658 0.511051    
## sd75                 9.967e-03  1.775e-02   0.562 0.574601    
## sd76                -2.916e-03  1.772e-02  -0.165 0.869294    
## sd77                -4.422e-03  1.772e-02  -0.249 0.803070    
## sd78                -1.433e-02  1.784e-02  -0.803 0.422043    
## sd79                 5.217e-03  1.777e-02   0.294 0.769155    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## 
## Residual standard error: 0.03751 on 635 degrees of freedom
## Multiple R-Squared: 0.9981,  Adjusted R-squared: 0.9978 
## F-statistic:  3541 on 95 and 635 DF,  p-value: < 2.2e-16 
## 
## 
## Estimation results for equation Diesel: 
## ======================================= 
## Diesel = Hydrous.Ethanol.l1 + Regular.Gasoline.l1 + LPG.l1 + Natural.Gas.l1 + Diesel.l1 + Hydrous.Ethanol.l2 + Regular.Gasoline.l2 + LPG.l2 + Natural.Gas.l2 + Diesel.l2 + Hydrous.Ethanol.l3 + Regular.Gasoline.l3 + LPG.l3 + Natural.Gas.l3 + Diesel.l3 + trend + sd1 + sd2 + sd3 + sd4 + sd5 + sd6 + sd7 + sd8 + sd9 + sd10 + sd11 + sd12 + sd13 + sd14 + sd15 + sd16 + sd17 + sd18 + sd19 + sd20 + sd21 + sd22 + sd23 + sd24 + sd25 + sd26 + sd27 + sd28 + sd29 + sd30 + sd31 + sd32 + sd33 + sd34 + sd35 + sd36 + sd37 + sd38 + sd39 + sd40 + sd41 + sd42 + sd43 + sd44 + sd45 + sd46 + sd47 + sd48 + sd49 + sd50 + sd51 + sd52 + sd53 + sd54 + sd55 + sd56 + sd57 + sd58 + sd59 + sd60 + sd61 + sd62 + sd63 + sd64 + sd65 + sd66 + sd67 + sd68 + sd69 + sd70 + sd71 + sd72 + sd73 + sd74 + sd75 + sd76 + sd77 + sd78 + sd79 
## 
##                       Estimate Std. Error t value Pr(>|t|)    
## Hydrous.Ethanol.l1  -1.768e-02  7.096e-02  -0.249 0.803291    
## Regular.Gasoline.l1 -1.160e-01  6.501e-02  -1.785 0.074782 .  
## LPG.l1              -1.344e-01  6.063e-02  -2.217 0.026982 *  
## Natural.Gas.l1       1.110e-01  3.515e-02   3.159 0.001656 ** 
## Diesel.l1            1.278e+00  5.148e-02  24.824  < 2e-16 ***
## Hydrous.Ethanol.l2   1.585e-01  1.266e-01   1.252 0.211137    
## Regular.Gasoline.l2 -4.529e-02  1.037e-01  -0.437 0.662573    
## LPG.l2               2.092e-01  9.482e-02   2.206 0.027746 *  
## Natural.Gas.l2      -1.265e-01  3.367e-02  -3.756 0.000189 ***
## Diesel.l2           -3.459e-01  7.951e-02  -4.351 1.58e-05 ***
## Hydrous.Ethanol.l3  -1.414e-01  7.071e-02  -1.999 0.046039 *  
## Regular.Gasoline.l3  1.345e-01  6.614e-02   2.033 0.042466 *  
## LPG.l3              -4.554e-02  6.163e-02  -0.739 0.460248    
## Natural.Gas.l3       7.090e-03  3.525e-02   0.201 0.840662    
## Diesel.l3            7.250e-02  5.249e-02   1.381 0.167718    
## trend                1.031e-05  3.705e-06   2.783 0.005553 ** 
## sd1                  7.351e-03  1.585e-02   0.464 0.642898    
## sd2                 -1.511e-03  1.584e-02  -0.095 0.924056    
## sd3                  1.005e-02  1.584e-02   0.635 0.525905    
## sd4                  1.262e-03  1.544e-02   0.082 0.934890    
## sd5                  3.784e-03  1.544e-02   0.245 0.806454    
## sd6                  7.538e-03  1.542e-02   0.489 0.625102    
## sd7                  1.839e-02  1.546e-02   1.190 0.234607    
## sd8                  2.543e-02  1.544e-02   1.647 0.100077    
## sd9                  1.478e-03  1.544e-02   0.096 0.923798    
## sd10                -5.091e-02  1.549e-02  -3.286 0.001071 ** 
## sd11                 3.333e-03  1.571e-02   0.212 0.831995    
## sd12                -2.821e-03  1.568e-02  -0.180 0.857308    
## sd13                 2.538e-03  1.546e-02   0.164 0.869619    
## sd14                 1.793e-03  1.585e-02   0.113 0.909993    
## sd15                 2.606e-03  1.587e-02   0.164 0.869571    
## sd16                 1.911e-02  1.586e-02   1.205 0.228694    
## sd17                 4.005e-03  1.588e-02   0.252 0.800911    
## sd18                 3.669e-03  1.582e-02   0.232 0.816715    
## sd19                 2.771e-03  1.586e-02   0.175 0.861383    
## sd20                -1.467e-03  1.585e-02  -0.093 0.926318    
## sd21                 1.350e-03  1.583e-02   0.085 0.932058    
## sd22                -2.432e-03  1.582e-02  -0.154 0.877863    
## sd23                 2.044e-03  1.585e-02   0.129 0.897454    
## sd24                 1.278e-02  1.583e-02   0.808 0.419608    
## sd25                -9.085e-03  1.583e-02  -0.574 0.566128    
## sd26                -3.989e-03  1.585e-02  -0.252 0.801347    
## sd27                -4.433e-04  1.588e-02  -0.028 0.977745    
## sd28                 3.970e-03  1.615e-02   0.246 0.805866    
## sd29                -1.519e-03  1.615e-02  -0.094 0.925087    
## sd30                 2.298e-02  1.596e-02   1.440 0.150477    
## sd31                 1.064e-02  1.591e-02   0.669 0.503738    
## sd32                -1.145e-03  1.590e-02  -0.072 0.942616    
## sd33                 4.608e-03  1.587e-02   0.290 0.771672    
## sd34                -2.307e-03  1.585e-02  -0.146 0.884352    
## sd35                -3.067e-03  1.583e-02  -0.194 0.846459    
## sd36                 6.243e-03  1.590e-02   0.393 0.694799    
## sd37                -4.035e-04  1.591e-02  -0.025 0.979773    
## sd38                -3.470e-03  1.589e-02  -0.218 0.827207    
## sd39                -6.865e-04  1.593e-02  -0.043 0.965640    
## sd40                -9.279e-04  1.591e-02  -0.058 0.953511    
## sd41                -8.018e-03  1.587e-02  -0.505 0.613512    
## sd42                -2.153e-03  1.589e-02  -0.135 0.892296    
## sd43                -1.706e-03  1.589e-02  -0.107 0.914534    
## sd44                -4.851e-03  1.587e-02  -0.306 0.759923    
## sd45                 2.391e-02  1.590e-02   1.504 0.133031    
## sd46                 5.480e-03  1.606e-02   0.341 0.733090    
## sd47                 3.628e-02  1.596e-02   2.274 0.023315 *  
## sd48                -6.487e-03  1.588e-02  -0.408 0.683063    
## sd49                 4.568e-03  1.594e-02   0.287 0.774513    
## sd50                 1.987e-03  1.586e-02   0.125 0.900323    
## sd51                 1.079e-02  1.585e-02   0.681 0.496198    
## sd52                 1.087e-03  1.585e-02   0.069 0.945357    
## sd53                 2.814e-02  1.586e-02   1.774 0.076461 .  
## sd54                -2.779e-03  1.590e-02  -0.175 0.861304    
## sd55                 1.036e-02  1.587e-02   0.652 0.514340    
## sd56                 6.323e-03  1.588e-02   0.398 0.690728    
## sd57                 2.398e-03  1.589e-02   0.151 0.880109    
## sd58                 8.310e-03  1.590e-02   0.523 0.601468    
## sd59                 2.834e-03  1.595e-02   0.178 0.859031    
## sd60                -3.013e-03  1.590e-02  -0.190 0.849725    
## sd61                 9.425e-03  1.589e-02   0.593 0.553400    
## sd62                 1.421e-04  1.588e-02   0.009 0.992861    
## sd63                 5.417e-03  1.586e-02   0.341 0.732873    
## sd64                 5.031e-04  1.589e-02   0.032 0.974747    
## sd65                 1.356e-02  1.586e-02   0.855 0.392844    
## sd66                 2.524e-03  1.585e-02   0.159 0.873545    
## sd67                 3.391e-03  1.588e-02   0.214 0.830921    
## sd68                 3.356e-03  1.584e-02   0.212 0.832292    
## sd69                 3.295e-02  1.587e-02   2.076 0.038278 *  
## sd70                 6.419e-04  1.589e-02   0.040 0.967790    
## sd71                 4.090e-03  1.587e-02   0.258 0.796696    
## sd72                 3.771e-03  1.584e-02   0.238 0.811851    
## sd73                 5.206e-04  1.582e-02   0.033 0.973761    
## sd74                -2.507e-04  1.584e-02  -0.016 0.987379    
## sd75                -2.792e-03  1.585e-02  -0.176 0.860192    
## sd76                -2.264e-03  1.582e-02  -0.143 0.886235    
## sd77                 2.242e-02  1.583e-02   1.417 0.156978    
## sd78                 5.506e-03  1.593e-02   0.346 0.729736    
## sd79                 9.216e-03  1.587e-02   0.581 0.561587    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## 
## Residual standard error: 0.03349 on 635 degrees of freedom
## Multiple R-Squared: 0.9987,  Adjusted R-squared: 0.9985 
## F-statistic:  5219 on 95 and 635 DF,  p-value: < 2.2e-16 
## 
## 
## 
## Covariance matrix of residuals:
##                  Hydrous.Ethanol Regular.Gasoline       LPG Natural.Gas
## Hydrous.Ethanol        5.382e-04        4.612e-04 1.958e-05   1.525e-05
## Regular.Gasoline       4.612e-04        1.050e-03 6.333e-05   8.447e-05
## LPG                    1.958e-05        6.333e-05 4.776e-04   5.707e-05
## Natural.Gas            1.525e-05        8.447e-05 5.707e-05   1.407e-03
## Diesel                 2.244e-04        6.910e-04 3.696e-05   5.607e-06
##                     Diesel
## Hydrous.Ethanol  2.244e-04
## Regular.Gasoline 6.910e-04
## LPG              3.696e-05
## Natural.Gas      5.607e-06
## Diesel           1.122e-03
## 
## Correlation matrix of residuals:
##                  Hydrous.Ethanol Regular.Gasoline     LPG Natural.Gas
## Hydrous.Ethanol          1.00000          0.61352 0.03863    0.017529
## Regular.Gasoline         0.61352          1.00000 0.08944    0.069508
## LPG                      0.03863          0.08944 1.00000    0.069634
## Natural.Gas              0.01753          0.06951 0.06963    1.000000
## Diesel                   0.28876          0.63674 0.05050    0.004464
##                    Diesel
## Hydrous.Ethanol  0.288760
## Regular.Gasoline 0.636744
## LPG              0.050499
## Natural.Gas      0.004464
## Diesel           1.000000
#plot(var.fit)
pred_VAR2 <- predict(var.fit2,df5_scaled2[,c(1:5)],n.ahead=52)
#plot(pred_VAR2)


# compute  the acf resduals 
acf1=ggAcf(residuals(var.fit2)[,1],200)
acf2=ggAcf(residuals(var.fit2)[,2],200)
acf3=ggAcf(residuals(var.fit2)[,3],200)
acf4=ggAcf(residuals(var.fit2)[,4],200)
acf5=ggAcf(residuals(var.fit2)[,5],200)
ggarrange(acf1, acf2,acf3,acf4,acf5,
          ncol = 1, nrow = 5)

for (i in 1:5){
  check_r=checkresiduals(residuals(var.fit2)[,i])
}

# > names(var.fit2$varresult)
# [1] "Hydrous.Ethanol" 
# [2] "Regular.Gasoline"
# [3] "LPG"             
# [4] "Natural.Gas"     
# [5] "Diesel" 
for (i in 1:5){
print(Box.test(residuals(var.fit2)[,i], type = "Ljung-Box"))}
## 
##  Box-Ljung test
## 
## data:  residuals(var.fit2)[, i]
## X-squared = 0.1318, df = 1, p-value = 0.7166
## 
## 
##  Box-Ljung test
## 
## data:  residuals(var.fit2)[, i]
## X-squared = 0.0013848, df = 1, p-value = 0.9703
## 
## 
##  Box-Ljung test
## 
## data:  residuals(var.fit2)[, i]
## X-squared = 0.015084, df = 1, p-value = 0.9023
## 
## 
##  Box-Ljung test
## 
## data:  residuals(var.fit2)[, i]
## X-squared = 0.9275, df = 1, p-value = 0.3355
## 
## 
##  Box-Ljung test
## 
## data:  residuals(var.fit2)[, i]
## X-squared = 0.00098154, df = 1, p-value = 0.975
#pred_VAR2$fcst
#df_undiff1 = data.frame(num = 1:52)


# add_trend = function(num,name){
# df_undiff = data.frame(idx = 1:52)
# for (i in name){
#   df_undiff[i] = df5[735,i]+cumsum(pred_VAR2$fcst[[i]][,num])}
# return(df_undiff)
# }

# undiff_avg = add_trend(1,name)
# undiff_lower = add_trend(2,name)
# undiff_upper = add_trend(3,name)

# data.frame( apply(df4[,2:6],2,scale))

# nor = function(v1){scales::rescale(v1, to=c(mean(v1)-0.5,max(v1)+0.5))}
# undiff_lower = data.frame( apply(undiff_lower[,2:6],2,nor))
# undiff_upper = data.frame( apply(undiff_upper[,2:6],2,nor))

#df_undiff1 = data.frame(num = 1:52)
# df_undiff1["Hydrous.Ethanol"] = df5$Hydrous.Ethanol[735]+cumsum(pred_VAR2$fcst[["Hydrous.Ethanol"]][,1])
# df_undiff1["Regular.Gasoline"] = df5$Hydrous.Ethanol[735]+cumsum(pred_VAR2$fcst[["Regular.Gasoline"]][,1])
# df_undiff1["Natural.Gas"] = df5$Hydrous.Ethanol[735]+cumsum(pred_VAR2$fcst[["Natural.Gas"]][,1])
# df_undiff1["LPG"] = df5$Hydrous.Ethanol[735]+cumsum(pred_VAR2$fcst[["LPG"]][,1])
# df_undiff1["Diesel"] = df5$Hydrous.Ethanol[735]+cumsum(pred_VAR2$fcst[["Diesel"]][,1])

Computing the metrics and ploting the result

col_prod2 = brewer.pal(12,"Paired")

name = c("Hydrous.Ethanol","Regular.Gasoline","Natural.Gas","LPG","Diesel")

plot_result = function(pred.var,i,c1,c2){
df_plotpred = data.frame(date=as.Date(df5$date),value = c(rep(NA,733),pred_VAR2$fcst[[i]][,1]))
df_plotpred['lower'] =  c(rep(NA,733),pred_VAR2$fcst[[i]][,2])
df_plotpred['upper'] = c(rep(NA,733),pred_VAR2$fcst[[i]][,3])
  
  vis = ggplot() +
    geom_line(data = df5,aes(date, Hydrous.Ethanol),col=col_prod2[c1]) +
      geom_line(data=df_plotpred,aes(date, value),col=col_prod2[c2])+
      geom_ribbon(data = df_plotpred,aes(x = date, ymax = upper, ymin = lower), alpha = 0.5, fill = col_prod2[c2])+rremove('xlab')+ theme_gray()

return(vis)
}

vis1_fit = plot_result(pred_VAR2,name[1],2,1)
vis2_fit = plot_result(pred_VAR2,name[2],4,3)
vis3_fit = plot_result(pred_VAR2,name[3],6,5)
vis4_fit = plot_result(pred_VAR2,name[4],8,7)
vis5_fit = plot_result(pred_VAR2,name[5],10,9)

ggarrange(vis1_fit, vis2_fit,vis3_fit,vis4_fit,vis5_fit, labels = c("Hydrous Ethanol",  "Regular Gasoline","LPG","Natural Gas","Diesel"), ncol = 3, nrow = 2, font.label = list(size = 10, face = "bold"))

#c("Hydrous.Ethanol","Hydrous.Ethanol","Regular.Gasoline","Regular.Gasoline","Natural.Gas","Natural.Gas","LPG","LPG","Diesel","Diesel")

# reording the 
df_acc_produc2t=  data.frame(product=c(rep(NA,10)),err_type=c(rep(NA,10)),ME=c(rep(NA,10)),RMSE=c(rep(NA,10)),MAE=c(rep(NA,10)),MPE=c(rep(NA,10)),MAPE=c(rep(NA,10)))
for (i in 1:5) {
df_acc_produc2t[i,]=c(name[i],"train",accuracy(var.fit2$varresult[[i]])[1:5])
df_acc_produc2t[i+5,]=c(name[i],"test",accuracy(pred_VAR2$fcst[[i]][,"fcst"],df5_scaled2[,c(1:5)][,i]))
}
#df_acc_produc2t
#write.csv(df_acc_product,"df_acc_product.csv" )

# ggplot(data=df_acc_product)+
#   geom_point(aes(product, RMSE))


df_acc_produc2t[,-c(1,2)] <- sapply(df_acc_produc2t[,-c(1,2)],as.numeric)

print(df_acc_produc2t, digits = 4)
##             product err_type         ME    RMSE      MAE      MPE   MAPE
## 1   Hydrous.Ethanol    train -1.274e-04 0.02164 0.014323   8.8169 15.336
## 2  Regular.Gasoline    train  7.453e-05 0.03022 0.015172   1.0028  4.285
## 3       Natural.Gas    train -8.667e-05 0.02038 0.009103   1.6804  4.020
## 4               LPG    train -1.631e-04 0.03498 0.025236   3.0698 13.830
## 5            Diesel    train -2.297e-05 0.03123 0.014716   0.5033  2.928
## 6   Hydrous.Ethanol     test -4.647e-01 0.53156 0.466161 -25.1495 25.225
## 7  Regular.Gasoline     test -1.991e-01 0.39598 0.332034 -10.4395 15.447
## 8       Natural.Gas     test -3.780e-01 0.42877 0.377999 -17.0151 17.015
## 9               LPG     test  4.850e-01 0.52692 0.484975  19.3837 19.384
## 10           Diesel     test  8.067e-02 0.19910 0.133131   3.4072  6.190
#read.csv("df_acc_product.csv" )

VAR: price info as exogenous variables

# function for extract the data 



#colnames(df4) = c('start_date',"Hydrous Ethanol",  "Regular Gasoline", "LPG","Natural Gas","Diesel" )
# which(df4$start_date == as.Date("2004-05-16"))


# acf1=ggAcf(HE["min_price"],200)
# acf2=ggAcf(HE["max_price"],200)
# acf3=ggAcf(HE["avg_price"],200)
# acf4=ggAcf(HE["coef_dist"],200)
# acf5=ggAcf(HE["sd_price"],200)
# ggarrange(acf1, acf2,acf3,acf4,acf5,
#           ncol = 1, nrow = 5)


name2 =unique(df3$fuel)



df_acc_avg =  data.frame(product=c(rep(NA,10)),err_type=c(rep(NA,10)),ME=c(rep(NA,10)),RMSE=c(rep(NA,10)),MAE=c(rep(NA,10)),MPE=c(rep(NA,10)),MAPE=c(rep(NA,10)))

for(i in 1:5){

  c1 = i*2
  c2  = i*2-1
  
  gas = name2[i]
  HE=df3[df3$fuel==gas,]
  HE['group'] = c(rep(1,785-52),rep(2,52))
  
  HE$start_date = as.Date( HE$start_date)
  HE_1=HE[HE['group'] ==1,]
  HE_2=HE[HE['group'] ==2,]

  var.fit_HE = VAR(HE_1[,c(3:7)], 5, type = "trend", ic = "AIC",season=80)
  summary(var.fit_HE)
  # check residual
  check_r=checkresiduals(residuals(var.fit_HE)[,3])
  box = Box.test(residuals(var.fit_HE)[,3], type = "Ljung-Box")
  print(box)
  
  pred.var_HE <- predict(var.fit_HE,HE_1[,c(3:7)],n.ahead=52)
  
  df_acc_avg[i,]=c(name[i],"train",accuracy(var.fit_HE$varresult[[3]])[1:5])
  df_acc_avg[i+5,]=c(name[i],"test",accuracy(pred.var_HE$fcst[[3]][,1],HE_2$avg_price))

  df_plotpred = data.frame(date=as.Date(HE$start_date),value = c(rep(NA,733),pred.var_HE$fcst[["avg_price"]][,1]))
  df_plotpred['lower'] =  c(rep(NA,733),pred.var_HE$fcst[["avg_price"]][,2])
  df_plotpred['upper'] = c(rep(NA,733),pred.var_HE$fcst[["avg_price"]][,3])


  vis.fit = ggplot() +
  geom_line(data = HE,aes(start_date, avg_price),col=col_prod2[c1]) +
  geom_line(data=df_plotpred,aes(date, value),col=col_prod2[c2])+
  geom_ribbon(data = df_plotpred,aes(x = date, ymax = upper, ymin = lower), alpha = 0.5, fill = col_prod2[c2])+
  rremove('xlab')+rremove("ylab")+theme_gray()

  acf.fit=ggAcf(residuals(var.fit_HE)[,3],200)
  assign(paste("vis", i, ".fit",sep = ""), vis.fit)
  assign(paste("acf", i, ".fit",sep = ""), acf.fit)
  assign(paste("checkr", i,sep = ""), check_r)
  assign(paste("box", i,sep = ""), box)
  
}

## 
##  Box-Ljung test
## 
## data:  residuals(var.fit_HE)[, 3]
## X-squared = 0.088645, df = 1, p-value = 0.7659

## 
##  Box-Ljung test
## 
## data:  residuals(var.fit_HE)[, 3]
## X-squared = 0.000648, df = 1, p-value = 0.9797

## 
##  Box-Ljung test
## 
## data:  residuals(var.fit_HE)[, 3]
## X-squared = 0.024801, df = 1, p-value = 0.8749

## 
##  Box-Ljung test
## 
## data:  residuals(var.fit_HE)[, 3]
## X-squared = 0.0084985, df = 1, p-value = 0.9265

## 
##  Box-Ljung test
## 
## data:  residuals(var.fit_HE)[, 3]
## X-squared = 9.0863e-05, df = 1, p-value = 0.9924
ggarrange(acf1.fit, acf2.fit,acf3.fit,acf4.fit,acf5.fit,
          ncol = 1, nrow = 5)

df_acc_avg[,-c(1,2)] <- sapply(df_acc_avg[,-c(1,2)],as.numeric)
print(df_acc_avg, digits = 2)
##             product err_type       ME  RMSE    MAE      MPE MAPE
## 1   Hydrous.Ethanol    train  2.6e-05 0.013 0.0086 -0.00036 0.39
## 2  Regular.Gasoline    train  1.4e-05 0.019 0.0097 -0.00152 0.32
## 3       Natural.Gas    train  1.6e-04 0.243 0.1173 -0.00171 0.26
## 4               LPG    train -6.0e-07 0.017 0.0125 -0.00749 0.68
## 5            Diesel    train  1.9e-06 0.017 0.0093 -0.00254 0.40
## 6   Hydrous.Ethanol     test -7.6e-02 0.107 0.0885 -2.25006 2.60
## 7  Regular.Gasoline     test -2.2e-01 0.314 0.2635 -5.15340 5.99
## 8       Natural.Gas     test -6.2e+00 6.792 6.2491 -8.66026 8.66
## 9               LPG     test  2.5e-01 0.278 0.2532  7.92604 7.95
## 10           Diesel     test  2.8e-02 0.109 0.0815  0.69713 2.21
is.num <- sapply(df_acc_avg, is.numeric)
df_acc_avg[is.num] <- lapply(df_acc_avg[is.num], round, 4)
print(as.matrix(df_acc_avg))
##       product            err_type ME        RMSE     MAE      MPE      
##  [1,] "Hydrous.Ethanol"  "train"  " 0.0000" "0.0130" "0.0086" "-0.0004"
##  [2,] "Regular.Gasoline" "train"  " 0.0000" "0.0193" "0.0097" "-0.0015"
##  [3,] "Natural.Gas"      "train"  " 0.0002" "0.2434" "0.1173" "-0.0017"
##  [4,] "LPG"              "train"  " 0.0000" "0.0174" "0.0125" "-0.0075"
##  [5,] "Diesel"           "train"  " 0.0000" "0.0175" "0.0093" "-0.0025"
##  [6,] "Hydrous.Ethanol"  "test"   "-0.0758" "0.1072" "0.0885" "-2.2501"
##  [7,] "Regular.Gasoline" "test"   "-0.2237" "0.3139" "0.2635" "-5.1534"
##  [8,] "Natural.Gas"      "test"   "-6.2491" "6.7924" "6.2491" "-8.6603"
##  [9,] "LPG"              "test"   " 0.2526" "0.2781" "0.2532" " 7.9260"
## [10,] "Diesel"           "test"   " 0.0281" "0.1087" "0.0815" " 0.6971"
##       MAPE    
##  [1,] "0.3914"
##  [2,] "0.3233"
##  [3,] "0.2612"
##  [4,] "0.6777"
##  [5,] "0.3953"
##  [6,] "2.6028"
##  [7,] "5.9929"
##  [8,] "8.6603"
##  [9,] "7.9479"
## [10,] "2.2119"
print(box1)
## 
##  Box-Ljung test
## 
## data:  residuals(var.fit_HE)[, 3]
## X-squared = 0.088645, df = 1, p-value = 0.7659
print(box2)
## 
##  Box-Ljung test
## 
## data:  residuals(var.fit_HE)[, 3]
## X-squared = 0.000648, df = 1, p-value = 0.9797
print(box3)
## 
##  Box-Ljung test
## 
## data:  residuals(var.fit_HE)[, 3]
## X-squared = 0.024801, df = 1, p-value = 0.8749
print(box4)
## 
##  Box-Ljung test
## 
## data:  residuals(var.fit_HE)[, 3]
## X-squared = 0.0084985, df = 1, p-value = 0.9265
print(box4)
## 
##  Box-Ljung test
## 
## data:  residuals(var.fit_HE)[, 3]
## X-squared = 0.0084985, df = 1, p-value = 0.9265
ggarrange(vis1.fit, vis2.fit,vis3.fit,vis4.fit,vis5.fit, labels = c("Hydrous Ethanol",  "Regular Gasoline","LPG","Natural Gas","Diesel"), ncol = 3, nrow = 2, font.label = list(size = 10, face = "bold"))

EDA *2

library(ggplot2)
library(forecast)
theme_set(theme_classic())

# Plot each product's price per year

library(RColorBrewer)
col_prod = brewer.pal(15,"BrBG")

brks = df5_scaled$date[seq(1, length(df5_scaled$date),52)]
lbls = unique(lubridate::year(df5_scaled$date))




df3_plot = df3
df3_plot['year']=lubridate::year(df3$start_date)
df3_plot['month']=month.abb[lubridate::month(df3$start_date)]
df3_plot$year=as.factor(df3_plot$year)

name2 =unique(df3_plot$fuel)

  for(i in 1:5){

    gas = name2[i]
    vis=ggplot(df3_plot[df3_plot$fuel==gas,])+
  geom_line(aes(x=month,y=scale(avg_price),group=year,col=year))+
 rremove('xlab')+rremove("ylab")
  theme_gray()# title 
    
  assign(paste("viss", i,sep = ""), vis)}

ggarrange(viss1, viss2,viss3,viss4,viss5, labels = c("Hydrous Ethanol",  "Regular Gasoline","LPG","Natural Gas","Diesel"), ncol = 2, nrow = 3, font.label = list(size = 10, face = "bold"))